package com.glorystone.ins.common;

import com.glorystone.ins.dao.ProductMpr;
import com.glorystone.ins.domain.Product;
import com.glorystone.ins.service.ProductSvr;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * 项目名称：ins
 * 类名称：TimedTask
 * 类描述：定时任务
 * 创建人：zgh
 * 创建时间：2018/3/25 17:01
 */
@Slf4j
public class TimedTask {

    @Autowired
    private ProductSvr productSvr;
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void TaskJob() throws ParseException {
        log.info("------------------定时任务开启-----------------------");
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = new Date().toInstant();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        System.out.println(localDate);
        List<Product> list = productSvr.findAllPriceList();
        for (Product product:list) {
            boolean flag = localDate.isAfter(product.getContractTime());
            System.out.println(flag);
            if (flag){
                product.setUseStatus(Product.UseEnum.USE_STATUS_OVERTIME.getValue());
                product.setCheckStatus(Product.CheckStatusEnum.CHECK_STATUS_OVERTIME.getValue());
                product.setUpdateDate(new Date());
                int i = productSvr.updateProductToOverTime(product);
                if (i>0){
                    log.info("----------修改成功id--------："+product.getTid()+"--------");
                }else{
                    log.info("---------修改失败产品id----------："+product.getTid()+"-----------------");
                }
            }
        }
        log.info("------------------定时任务结束-----------------------");
    }
}
